package com.ctrip.ec.api.admin.controller.album;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.dao.image.ImageDao;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.album.AlbumImageDao;
import com.ctrip.ec.biz.entity.album.AlbumImageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
@RestController
@RequestMapping("albumImage")
public class AlbumImageController{
    @Autowired
    private AlbumImageDao albumImage;

    @Value("${imageDomain}")
    private  String imageDomain;

    @Autowired
    private ImageDao image;


    @GetMapping("list")
    public List<AlbumImageEntity> list() throws SQLException {
        List<AlbumImageEntity> list = albumImage.list();
        return list;
    }

    @PostMapping("list")
    public List<AlbumImageEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<AlbumImageEntity> list = albumImage.list(setting);
//        for (AlbumImageEntity entity : list){
//            entity.setImageUrl(imageDomain + entity.getImageUrl());
//        }
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @GetMapping("detail/{id}")
    public  AlbumImageEntity detail(@PathVariable Long id) throws SQLException {
        return albumImage.entity(id);
    }

    @PostMapping("save")
    public  Long save(@RequestBody AlbumImageEntity entity) throws SQLException {
        albumImage.save(entity);
        image.inUse(entity.getImageId());
        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        List<Long> imageIds = albumImage.getImageIds(ids);
        image.unUse(imageIds);
        albumImage.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        albumImage.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        albumImage.unerase(ids);
    }
}
